/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.allword.service.impl;

import me.zhengjie.modules.allword.domain.StuWorkall;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.allword.repository.StuWorkallRepository;
import me.zhengjie.modules.allword.service.StuWorkallService;
import me.zhengjie.modules.allword.service.dto.StuWorkallDto;
import me.zhengjie.modules.allword.service.dto.StuWorkallQueryCriteria;
import me.zhengjie.modules.allword.service.mapstruct.StuWorkallMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author wzy
* @date 2024-12-12
**/
@Service
@RequiredArgsConstructor
public class StuWorkallServiceImpl implements StuWorkallService {

    private final StuWorkallRepository stuWorkallRepository;
    private final StuWorkallMapper stuWorkallMapper;

    @Override
    public PageResult<StuWorkallDto> queryAll(StuWorkallQueryCriteria criteria, Pageable pageable){
        Page<StuWorkall> page = stuWorkallRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(stuWorkallMapper::toDto));
    }

    @Override
    public List<StuWorkallDto> queryAll(StuWorkallQueryCriteria criteria){
        return stuWorkallMapper.toDto(stuWorkallRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public StuWorkallDto findById(Integer id) {
        StuWorkall stuWorkall = stuWorkallRepository.findById(id).orElseGet(StuWorkall::new);
        ValidationUtil.isNull(stuWorkall.getId(),"StuWorkall","id",id);
        return stuWorkallMapper.toDto(stuWorkall);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(StuWorkall resources) {


        stuWorkallRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(StuWorkall resources) {
        StuWorkall stuWorkall = stuWorkallRepository.findById(resources.getId()).orElseGet(StuWorkall::new);
        ValidationUtil.isNull( stuWorkall.getId(),"StuWorkall","id",resources.getId());
        stuWorkall.copy(resources);
        stuWorkallRepository.save(stuWorkall);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            stuWorkallRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<StuWorkallDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (StuWorkallDto stuWorkall : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("学号", stuWorkall.getStuid());
            map.put("学生姓名", stuWorkall.getStuname());
            map.put("课程号", stuWorkall.getClassid());
            map.put("得分", stuWorkall.getScore());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}
